package com.wuhunyu.service.impl;

import com.wuhunyu.commonutils.check.Check;
import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.entity.User;
import com.wuhunyu.entity.vo.UserVo;
import com.wuhunyu.mapper.UserMapper;
import com.wuhunyu.query.UserQuery;
import com.wuhunyu.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuhunyu.servicebase.exception.Assert;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User selectByUsername(String username) {
        return baseMapper.selectOne(new QueryWrapper<User>().eq("username", username));
    }

    @Override
    public CommonResult selectAllUser(UserQuery userQuery) {
        // 校验参数
        Assert.isRight(Check.isBlank(userQuery), "用户查询参数对象不能为空");
        // 对分页参数进行处理
        userQuery.setCurrent((userQuery.getCurrent() - 1) * userQuery.getLimit());
        // 执行查询操作
        List<UserVo> userVos = userMapper.selectAllUser(userQuery);
        Integer count = userMapper.selectAllUserForCount(userQuery);
        // 校验参数
        Assert.isRight(userVos == null || userVos.size() == 0, "查询用户数据为空");
        Assert.isRight(count < userVos.size(), "查询结果有误");
        // 封装数据
        Map<String, Object> map = new HashMap<>(16);
        map.put("users", userVos);
        map.put("total", count);
        return CommonResult.success("查询用户角色资源信息成功", map);
    }
}
